app.controller("publishCtrl", ["$scope", "$http", "$filter", "$rootScope", "$stateParams", "$timeout", "$modal", "lodash",
function($scope, $http, $filter, $rootScope, $stateParams, $timeout, $modal, lodash) {
    $rootScope.initBreadcrumb($scope.breadcrumb);
    $rootScope.initHeaderElements($scope.settings);
    $scope.breadcrumb.push('发布面板');

    $scope.settings.searchEle = {
        show: true, //显示查询框
        func: function (keyword) {
            seachPublish(keyword);
        },
        blurFunc: function (keyword) {
            seachPublish(keyword);
        }
    };

    $scope.info = {
        // 原始区域数据
        originPublishArray: [
            {"id": "1", "app": "oms1", "type": "tomcat01", "url": "192.168.157.156", "order": "1", "status": "running", "version": "v20180608"},
            {"id": "2", "app": "oms2", "type": "tomcat02", "url": "192.168.157.156", "order": "2", "status": "stop", "version": "v20180608"},
            {"id": "3", "app": "oms3", "type": "tomcat03", "url": "192.168.157.156", "order": "3", "status": "deploy", "version": "v20180608"},
            {"id": "4", "app": "oms4", "type": "tomcat04", "url": "192.168.157.156", "order": "4", "status": "checking", "version": "v20180608"},
            {"id": "5", "app": "oms5", "type": "tomcat05", "url": "192.168.157.156", "order": "5", "status": "error", "version": "v20180608"},
            {"id": "6", "app": "oms6", "type": "tomcat06", "url": "192.168.157.156", "order": "6", "status": "running", "version": "v20180608"}
        ],
        originConfigTypeArray: [
            {"key": "tomcat01", "value": "tomcat-dubbo-01"},
            {"key": "tomcat02", "value": "tomcat-dubbo-02"},
            {"key": "tomcat03", "value": "tomcat-dubbo-03"},
            {"key": "tomcat04", "value": "tomcat-dubbo-04"},
            {"key": "tomcat05", "value": "tomcat-dubbo-05"},
            {"key": "tomcat06", "value": "tomcat-dubbo-06"}
        ],
        publishStatusList: [
            {"key": "running", "value": "运行中"},
            {"key": "stop", "value": "已停止"},
            {"key": "deploy", "value": "部署中"},
            {"key": "checking", "value": "验证中"},
            {"key": "error", "value": "验证失败"},
        ],
        allAppList: [
            {"id": "1", "name": "oms1"},
            {"id": "2", "name": "oms2"},
            {"id": "3", "name": "oms3"},
            {"id": "4", "name": "oms4"},
            {"id": "5", "name": "oms5"},
            {"id": "6", "name": "oms6"}
        ],
        allAreaList: [],
        publishList: [],
    };

    //设置data-table
    $scope.options = {
        rowHeight: 40,
        headerHeight: 40,
        footerHeight: 50,
        columnMode: 'force',
        forceFillColumns: true,
        emptyMessage: '暂无数据！',
        columns: [
            { name: "app", width: 100, prop: 'app'},
            { name: "类型", width: 100, prop: 'typeName'},
            { name: "url", width: 80, prop: 'url'},
            { name: "启动顺序", width: 80, prop: 'order'},
            { name: "状态", width: 80, 
              cellRenderer: function() {
                  return '<span ng-style="{\'color\': ($row.status == \'running\' ? \'green\' : ($row.status == \'error\' ? \'red\' : \'black\'))}">{{ $row.statusName }}</span>';
              }
            },
            { name: "版本", width: 80, prop: 'version'},
            {
                name: "操作",
                width: 100,
                cellRenderer: function () {
                    return '<a href="javascript:void(0);" style="margin-right: 10px;" ng-click="deployItem($row)" ng-if="$row.status != \'deploy\' && $row.status != \'checking\'">部署</a>' +
                           '<a href="javascript:void(0);" style="margin-right: 10px;" ng-click="stopItem($row)" ng-if="$row.status == \'running\' || $row.status == \'checking\'">停止</a>' +
                           '<a href="javascript:void(0);" style="margin-right: 10px;" ng-click="infoItem($row)">日志</a>';
                }
            }
        ],
    };

    initAreaList();
    setTableData();
    setAllApp();

    function initAreaList() {
        var allAreaList = new Array();
        angular.forEach(config_area_list, function(area) {
            allAreaList.push(area);
            angular.forEach(config_warehouse_list, function(warehouse) {
                if(area.areaName == warehouse.type) {
                    allAreaList.push(warehouse);
                }
            });
        });
        $scope.info.allAreaList = allAreaList;
        setAllArea();
    }

    // 全部应用
    function setAllApp() {
        var appList = [{"crtname": "全部应用", "index": 0}];
        angular.forEach($scope.info.allAppList, function(app) {
            app.crtname = app.name;
            appList.push(app);
        });
        var originSelectEle = {
            status: true,
            items: appList,
            prompt: "请选择应用",
            selectedItem: {"crtname": "全部应用", "key": 0},
            func: function (item) {
                seachPublish(item.name);
            }
        };

        $scope.settings.uiSltEle.push(originSelectEle);
    }

    // 全部区域
    function setAllArea() {
        var areaList = [{"crtname": "全部区域", "index": 0}];
        angular.forEach($scope.info.allAreaList, function(area) {
            area.crtname = area.zone_title;
            areaList.push(area);
        });
        var originSelectEle = {
            status: true,
            items: areaList,
            prompt: "请选择应用",
            selectedItem: {"crtname": "全部区域", "key": 0},
            func: function (item) {
                seachPublish(item.zone_title);
            }
        };

        $scope.settings.uiSltEle.push(originSelectEle);
    }

    // 获取数据
    function setTableData() {
        $timeout(function() {
            angular.forEach($scope.info.originPublishArray, function(publishItem) {
                // 设置类型名称
                var typeItem = lodash.findWhere($scope.info.originConfigTypeArray, {key: publishItem.type});
                publishItem.typeName = typeItem ? typeItem.value : "-";
                // 设置类型状态
                var statusItem = lodash.findWhere($scope.info.publishStatusList, {key: publishItem.status});
                publishItem.statusName = statusItem ? statusItem.value : "-";
            });
            $scope.tableData = $scope.info.originPublishArray;
        }, 200);
    };

    // 查询
    function seachPublish(keyword) {
        var searchPublishData = new Array();
        angular.forEach($scope.info.originPublishArray, function(item) {
            if(item.app == keyword || item.typeName == keyword || item.url == keyword || item.order == keyword || item.statusName == keyword || item.version == keyword) {
                searchPublishData.push(item);
            }
        });
        $scope.tableData = searchPublishData.length ? searchPublishData : $scope.info.originPublishArray;
    };

    // 部署
    $scope.deployItem = function(row) {
        openModal(row);
    };

    // 停止
    $scope.stopItem = function(row) {
        var confirm = {
            titleData: "提示",
            bodyData: "确定要停止【"+ row.app +"】吗？"
        };
        $rootScope.confirmWindow(confirm, function (data) {
            if(data){
                var stopItem = lodash.findWhere($scope.info.originPublishArray, {id: row.id});
                stopItem.status = "stop";
                stopItem.statusName = lodash.findWhere($scope.info.publishStatusList, {key: "stop"}).value;
                $rootScope.toasterSuccessInfo("停止成功！");
            }
        })
    };

    // 日志
    $scope.infoItem = function(row) {
        $rootScope.toasterWarningInfo("查看日志开发中。。。");
    };

    // 打开modal
    function openModal(row) {
        var modalInstance = $modal.open({
            animation: true,
            templateUrl: getTemplateUrl("matrix/ucloud2.0/tpl/publishModal.html"),
            controller: "publishModalCtrl",
            keyboard: true,
            backdrop: true,
            resolve: {
                row: function() {
                    return row;
                }
            }
        });

        modalInstance.result.then(function (publishItem) {
            ajaxFun(publishItem);
        }, function (reason) {
            console.log("关闭modal");
        });
    };
        
    // 更新数据
    function ajaxFun(publishItem) {
        var updateItem = lodash.findWhere($scope.info.originPublishArray, {id: publishItem.id});
        updateItem.status = "deploy";
        updateItem.statusName = lodash.findWhere($scope.info.publishStatusList, {key: "deploy"}).value;
        updateItem.version = publishItem.version;
        $rootScope.toasterSuccessInfo("操作成功！");
    };

}]);

app.controller("publishModalCtrl", ["$scope", "$rootScope", "$modalInstance", "$http", "lodash", "row", function ($scope, $rootScope, $modalInstance, $http, lodash, row) {
    $scope.info = {
        branchList: [
            {"id": "1", "name": "分支001"},
            {"id": "2", "name": "分支002"},
            {"id": "3", "name": "分支003"},
            {"id": "4", "name": "分支004"}
        ],
        sltbranchItem: "",
        defaultBranch: true,     // 默认选中【依照项目分支】
        checkRisk: false,        // 默认不选
    }

    initBranchDetails();

    // 初始化数据
    function initBranchDetails() {
        angular.forEach($scope.info.branchList, function(branch) {
            branch.crtname = branch.name;
        });
    };

    // 依照项目分支
    $scope.changeCheckBox = function(status) {
        if(status) {
            $scope.info.sltbranchItem = "";
        }
    };

    $scope.createClick = function () {
        if(!$scope.info.defaultBranch) {
            if(!$scope.info.sltbranchItem) {
                $rootScope.toasterWarningInfo("请选择分支/版本");
                return false;
            }
            row.version = $scope.info.sltbranchItem.name;
        }
        $modalInstance.close(row);
    };

    $scope.cancelClick = function () {
        $modalInstance.dismiss("cancel");
    };
}]);
